package cn.toolck.minitomcat.pojo;

import cn.toolck.minitomcat.container.Container;
import cn.toolck.minitomcat.container.Host;
import cn.toolck.minitomcat.map.Mapper;
import cn.toolck.minitomcat.util.HttpProtocolUtil;
import cn.toolck.minitomcat.util.StaticResourceUtil;
import cn.toolck.minitomcat.servlet.Servlet;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Collection;
import java.util.Locale;

public class Response implements HttpServletResponse {
    private OutputStream out;

    public Response(OutputStream out) {
        this.out = out;
    }

    public Response() {
    }

    public void outputHtml(String path) throws Exception {
        String realPath = StaticResourceUtil.getAbsolutePath(path);
        InputStream resourceAsInputStream = StaticResourceUtil.getResourceAsInputStream(realPath);
        if (resourceAsInputStream == null) {
            out.write(HttpProtocolUtil.getHttpContext404().getBytes());
            return;
        }
        int count = 0;
        while (count == 0) {
            count = resourceAsInputStream.available();
        }
        out.write(HttpProtocolUtil.getHttpHead200(count).getBytes());
        StaticResourceUtil.writeToOutPutStreamFromInputStream(resourceAsInputStream, out);
    }


    public OutputStream getOut() {
        return out;
    }

    public void setOut(OutputStream out) {
        this.out = out;
    }

    public void output(String context) {
        try {
            out.write(HttpProtocolUtil.getHttpContext200(context).getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addCookie(Cookie cookie) {

    }

    @Override
    public boolean containsHeader(String name) {
        return false;
    }

    @Override
    public String encodeURL(String url) {
        return null;
    }

    @Override
    public String encodeRedirectURL(String url) {
        return null;
    }

    @Override
    public String encodeUrl(String url) {
        return null;
    }

    @Override
    public String encodeRedirectUrl(String url) {
        return null;
    }

    @Override
    public void sendError(int sc, String msg) throws IOException {

    }

    @Override
    public void sendError(int sc) throws IOException {

    }

    @Override
    public void sendRedirect(String location) throws IOException {

    }

    @Override
    public void setDateHeader(String name, long date) {

    }

    @Override
    public void addDateHeader(String name, long date) {

    }

    @Override
    public void setHeader(String name, String value) {

    }

    @Override
    public void addHeader(String name, String value) {

    }

    @Override
    public void setIntHeader(String name, int value) {

    }

    @Override
    public void addIntHeader(String name, int value) {

    }

    @Override
    public void setStatus(int sc) {

    }

    @Override
    public void setStatus(int sc, String sm) {

    }

    @Override
    public int getStatus() {
        return 0;
    }

    @Override
    public String getHeader(String name) {
        return null;
    }

    @Override
    public Collection<String> getHeaders(String name) {
        return null;
    }

    @Override
    public Collection<String> getHeaderNames() {
        return null;
    }

    @Override
    public String getCharacterEncoding() {
        return null;
    }

    @Override
    public String getContentType() {
        return null;
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        return null;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        return null;
    }

    @Override
    public void setCharacterEncoding(String charset) {

    }

    @Override
    public void setContentLength(int len) {

    }

    @Override
    public void setContentLengthLong(long len) {

    }

    @Override
    public void setContentType(String type) {

    }

    @Override
    public void setBufferSize(int size) {

    }

    @Override
    public int getBufferSize() {
        return 0;
    }

    @Override
    public void flushBuffer() throws IOException {

    }

    @Override
    public void resetBuffer() {

    }

    @Override
    public boolean isCommitted() {
        return false;
    }

    @Override
    public void reset() {

    }

    @Override
    public void setLocale(Locale loc) {

    }

    @Override
    public Locale getLocale() {
        return null;
    }

    public void outputHtml(String url, Mapper.MappedContext mappedContext) {
        try {
            Host host = (Host) mappedContext.getObj().getParent();
            String appBase = host.getAppBase();
            String docBase = mappedContext.getObj().getDocBase();
            String path = appBase + docBase + url;
            File file = new File(path);
            if (!file.exists()) {
                out.write(HttpProtocolUtil.getHttpContext404().getBytes());
                return;
            }
            FileInputStream resourceAsInputStream = new FileInputStream(file);
            if (resourceAsInputStream == null) {
                out.write(HttpProtocolUtil.getHttpContext404().getBytes());
                return;
            }
            int count = 0;
            while (count == 0) {
                count = resourceAsInputStream.available();
            }
            out.write(HttpProtocolUtil.getHttpHead200(count).getBytes());
            StaticResourceUtil.writeToOutPutStreamFromInputStream(resourceAsInputStream, out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
